/*
 * Copyright 2007-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.springextensions.actionscript.userextensions.flex
{
	import mx.core.IFactory;
	
	import org.springextensions.actionscript.context.IApplicationContext;
	import org.springextensions.actionscript.context.IApplicationContextAware;	
	 
	/**
	 * A ClassRendererFactory instance is a "factory object", can be used for any List controls in flex to generate instances of renderer class
	 * to be injected 
	 * <code>ClassRendererFactory</code> is used to inject renderers(itemRenderers,headerRenderers) for any list based controls in flex
	 * <p>For example, suppose you write an item renderer class named 
 	 *  CheckBoxRenderer extends CheckBox
 	 * if you want to make a List control use this renderer, and have each renderer
 	 *  instance display a CheckBox such way that can be injected via Spring, you would write the following code:</p>
 	 * 
 	 * <pre>
 	 *  //You define the following in applicationContext.xml 
 	 *  //  create renderer Factories and each item renderer factory in turn can be configured to inject an actual item renderer via setter injection. 
 	 *  <object id="listRendererFactory"  class="org.springextensions.actionscript.objects.ClassRendererFactory" >
	 *	 <property name="rendererID" value="checkBoxItemRenderer"/>	 	
	 *	</object>
	 * 
	 * <object id="checkBoxItemRenderer"  class="renderers.CheckBoxItemRenderer"  scope="prototype" />
	 * 
 	 * // type cast to IFactory is necessary to set renderers in flex				
	 * list.itemRenderer = IFactory(applicationContext.getObject('listRendererFactory'));</pre>	 
	 * 
	 * @author Santosh Kumar
	 * @version 1.0
	*/
	
	 
	public class ClassRendererFactory implements IFactory,IClassRendererFactory,IApplicationContextAware
	{
		
		private var _applicationContext:IApplicationContext;		
		private var _rendererID:String;		
		
		public function ClassRendererFactory()
		{
			super();
		}
		
			
	/** 
	*	applicationContext is automatically injected via IApplicationContextAware interface that is provided with a reference to the
	*	applicationContext that created it, when it is created by 
	*	ApplicationContext.
	*/	
		public  function set applicationContext(value:IApplicationContext):void
		{
			_applicationContext = value;
		
		}	
		
	/**       
	 * @return _applicationContext that was set
    */		
		public  function get applicationContext():IApplicationContext
		{
			return _applicationContext;
				
		}	
	
	
		
	/**
     *  rendererID is used to identify the correct renderer object instance for any list control  
	 *  to create renderers from this factory object.
    */
    
    
		public  function set rendererID(id:String):void
		{
			if(_rendererID !== id)
			{
				_rendererID = id;
			}
				
		}
		
	/**       
	 * @return The new rendererID that was set
    */
    	
		public function get rendererID():String
		{
			return _rendererID;
		}

 	/**  
	 * <p>This method implements the <code>newInstance()</code> method
	 *  of the IFactory interface.</p>
	 *  @return the new spring renderer object instance that was created.  
	 */
	 
	 	public function newInstance():*
		{
			
			var rendererInstance:* =  _applicationContext.getObject(_rendererID);		
			return rendererInstance;
		}
		
	}
}